#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Houzz Web服务启动脚本
"""

import os
import sys
import subprocess
from pathlib import Path

def check_dependencies():
    """检查依赖包"""
    try:
        import flask
        import pandas
        import redis
        import requests
        import lxml
        print("✅ 所有依赖包已安装")
        return True
    except ImportError as e:
        print(f"❌ 缺少依赖包: {e}")
        print("请运行: pip install -r requirements.txt")
        return False

def create_directories():
    """创建必要的目录"""
    directories = ['exports', 'logs/houzz', 'templates']
    for directory in directories:
        Path(directory).mkdir(parents=True, exist_ok=True)
        print(f"✅ 目录已创建: {directory}")

def start_web_service():
    """启动Web服务"""
    print("🚀 启动Houzz爬虫Web服务...")
    print("=" * 50)
    print("访问地址: http://localhost:8899/")
    print("按 Ctrl+C 停止服务")
    print("=" * 50)
    
    try:
        from web_service import app
        print("✅ Houzz爬虫服务已准备就绪")
        app.run(host='0.0.0.0', port=8899, debug=False)
    except KeyboardInterrupt:
        print("\n👋 服务已停止")
    except Exception as e:
        print(f"❌ 启动失败: {e}")
        sys.exit(1)

if __name__ == '__main__':
    print("Houzz爬虫Web服务启动器")
    print("=" * 30)
    
    # 检查依赖
    if not check_dependencies():
        sys.exit(1)
    
    # 创建目录
    create_directories()
    
    # 启动服务
    start_web_service()
